package com.clw.weblogin.service.impl;

import com.clw.weblogin.common.entity.ResultEntity;
import com.clw.weblogin.dao.UserEntityMapper;
import com.clw.weblogin.entity.UserEntity;
import com.clw.weblogin.entity.UserEntityExample;
import com.clw.weblogin.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务接口实现类
 *
 * @author clw
 * @create 2018-01-20 10:31
 **/
@Service
public class IUserSeiviceImpl implements IUserService  {

    @Autowired
    private UserEntityMapper mUserEntityMapper;

    public ResultEntity login(UserEntity userEntity) {
        ResultEntity resultEntity=new ResultEntity();
        //Example类指定如何构建一个动态的where子句.
        UserEntityExample example=new UserEntityExample();
        //创建SQL语句条件对象
        UserEntityExample.Criteria criteria=example.createCriteria();
        //添加SQL语句where子句的条件：相当于 select * from tb_user where username=123 and password=123;
        criteria.andUsernameEqualTo(userEntity.getUsername());
        criteria.andPasswordEqualTo(userEntity.getPassword());
        example.or(criteria);
        //执行查询，返回结果
        List<UserEntity> list=mUserEntityMapper.selectByExample(example);
        if(list.size()>0){
            resultEntity.setState(200);
            resultEntity.setMessage("登录成功");
            resultEntity.setData(list.get(0));
        }else{
            resultEntity.setMessage("登录失败");
        }
        return resultEntity;
    }
}
